﻿//我的收藏的js
//var myScroll;
var value=1;
var m=0;
var n=0;
var Count=0;
var Count1=0;
var appID;
var getGuid;
var appEName;
var UserID;
var getUserName;
var _ischeck=false;
require.config({
    baseUrl: '/js/common', 
    paths: {
        libs: '/js/libs'
    }
});
define(function(require){
	// 加载js
	var rs_zepto = require('libs/zepto');
	var rs_yingSoftCookie = require('yingSoftCookie');
	var rs_yingSoftPrg = require('yingSoftPrg');
	var rs_agentInfo = require('agentInfo');
	var rs_ajax = require('ajax');
	var rs_md5 = require('md5');
	var rs_dialog = require('dialog');
	var rs_storage = require('storage');
	var rs_yingSoftSetTest =require('yingSoftSetTest');
	appID = G_Cookie.getAppID();
	getGuid=G_Cookie.getGuid();
    appEName = G_Cookie.getAppEName();
    UserID=G_Cookie.getUserID();
    getUserName=G_Cookie.getUserName();
	// 初始化
	var _isClick = true;	// 防止多次点击
	$('#loadingDiv').css('display','block');
	if(G_Prg.getQueryString('redoSwitch')==null){
		G_Prg.popMsg('您当前处于做题模式');
		myProblem(0);
	}
	if(G_Prg.getQueryString('redoSwitch')=='0'){
		$("#tab_problem").attr('data-value','2').text("背题模式");
		$('#problem div').eq(0).addClass("problme_active").siblings().removeClass("problme_active");
		G_Prg.popMsg('您当前处于做题模式');
		myProblem(0);
	}else if(G_Prg.getQueryString('redoSwitch')=='1'){
		$("#tab_problem").attr('data-value','2').text("背题模式");
		$('#problem div').eq(1).addClass("problme_active").siblings().removeClass("problme_active");
		G_Prg.popMsg('您当前处于做题模式');
		myProblemRanking(0);
	}
	qie();
	beiti();//切换
	tiaozhuan();
});
function myProblem(m) {//我的收藏	
	$('#pullUp').css('display','none');
	$('.noNum').css('display','none');
	$("#list_problem1").css('display','none');
	$("#list_problem").css('display','block');
	$('#loadingDiv').css('display','block');
    var _success = function (json) {
    	var jsonData = JSON.parse(json);
    	if(jsonData.status==201){
    		
    	}else if(jsonData.status==202){
    		$('.noNum').css('display','block');
    		$('.noNum').text('获取数据失败');
    	}else if(jsonData.status==200){
    		if(m==0&&jsonData.data.count==0){
    			$('.noNum').css('display','block');
    			$('.noNum').text('您还没有收藏');
    		}else{
    			Count=jsonData.data.count;
    			var datareplace=G_SetTestInfo.replaceImgURL(jsonData.data.test)
    			var data=datareplace.StyleItems;
    			if(Count>=50){
	    			$('#pullUp').css('display','block');
	    		}
	    		for(var i=0; i<data.length;i++){
	    			var infos=jsonData.data.info[i];
	    			var title='';
	    			var answer='';
	    			var list=data[i].TestItems[0];
	    			var explain='';
	    			if(data[i].Type=='A3TEST'){
	    				var titleF=list.A3TestItems[0];
	    				title=list.FrontTitle+titleF.Title;
	    				var Answers=titleF.Answer.split('');
						for(var j=0;j<titleF.SelectedItems.length;j++){
							for(var s=0;s<Answers.length;s++){
								if(Answers[s]==titleF.SelectedItems[j].ItemName){
									answer+=titleF.SelectedItems[j].Content+';';
								}
							}
	    				}
						explain=titleF.Explain;
	    			}else if(data[i].Type=='ATEST'||data[i].Type=='BTEST'||data[i].Type=='XTEST'){
	    				title=list.Title;
	    				answer='';
	    				var Answers=list.Answer.split('');
						for(var j=0;j<list.SelectedItems.length;j++){
							for(var s=0;s<Answers.length;s++){
								if(Answers[s]==list.SelectedItems[j].ItemName){
									answer+=list.SelectedItems[j].Content+';';
								}
							}
	    				}
						explain=list.Explain;
	    			}else{
	    				title=list.Title;
	    				answer=list.Answer;
						explain=list.Explain;
	    			}
	    			
	    			strLi = "<li  allTestID='"+infos.AllTestID+"' childTableID='"+infos.ChildTableID+"' childTableName='"+data[i].Type+"' cptID='"+infos.CptID+"' srcID='"+infos.SrcID+"' SbjID='"+infos.SbjID+"' StyleID='"+infos.StyleID+"' page='"+m+"'>" 
					+ "<div class='problme_title' data-display='none' data-flase='true' ><span id=''>"+(i+1)+".</span><span class='detail_problem'>"+title+"</span>"
					+ "<div class='problme_key pro_key'>"
					+ "<div class='jie_da'><span>答</span>"
					+ "<p>"+answer+"</p>" 
					+ "</div>" 
					+ "<div class='jie_da jie2'><span>解</span>"
					+ "<p>"+explain+"</p>" 
					+ "</div>" 
					+ "</div>" 
					+ "</li>";
					$("#list_problem").append(strLi);
	    		}
    		}  		
    	}
    	for(var i=0;i<50;i++){
			$('.jie2').each(function(){
				if($(this).find('p').text()==0||$(this).find('p').text()=='null'){
					$(this).remove();
				}
			})
		}
		myscroll.refresh();
	 	display_none();
	 	tiaozhuan();
    }
    var URL = '/api/exam/getUserFav';
    G_AjaxApi_Record.post(URL,{
    	appID:appID,
//	     appEName:appEName,
	     curPage:m,
	     guid:getGuid,
	     cptID:-1,
	     eachPageNum:50,
	     queryTestInfo:0,
	     queryKnoledge:0
    }, true, _success);
}
//背题模式和答题模式的切换
function beiti(){
	$("#tab_problem").on("touchend",function(){
		value=$(this).attr("data-value");
		m=0;
		n=0;
		$("#list_problem1").empty();
		$("#list_problem").empty();
//		$("#problem div:eq(0)").addClass("problme_active").siblings().removeClass("problme_active");
//		$("#problem div:eq(0)").css('color','#fff').siblings().css('color','#000');
		if($(this).attr("data-value")=="1"){	
			$(this).text("背题模式");					
			$(this).attr("data-value","2");
			G_Prg.popMsg('您当前处于做题模式');
			if($('#problem .problme_active').index()==0){
				myProblem(0);
			}else{
				myProblemRanking(0);
			}
			qie();
		}else{		
			$(this).text("做题模式");
			$(this).attr("data-value","1");
			G_Prg.popMsg('您当前处于背题模式');
			if($('#problem .problme_active').index()==0){
				myProblem(0);
			}else{
				myProblemRanking(0);
			}
			qie();
		}		
	})
}
//我的错题 和错题排行  的切换
function qie(){
	var URL = '/api/user/getVipMsg/' + UserID  + '/' + appEName  + '/' + getGuid;
	$("#problem div").unbind("click").click(function() {
		$("#list_problem1").empty();
		$("#list_problem").empty();
		$('#pullUp').css('display','none');
		if ($(this).index() == 0) {
			$(this).addClass("problme_active").siblings().removeClass("problme_active");
			$(this).css('color','#fff').siblings().css('color','#000');
			if($('#tab_problem').attr('data-value')==1){
				G_Prg.popMsg('您当前处于背做题模式');
			}else{
				G_Prg.popMsg('您当前处于做题模式');
			}
			$("#list_problem1").empty();
			$("#list_problem").empty();
			myProblem(0);			
		}else{
			var _success = function (json) { //G_AjaxApi.get的回调函数
		        //接收端口返回的数据,转换成json格式
		        var jsonData = JSON.parse(json);
		        if (jsonData.status === 200) {
		        	$("#problem div").eq(1).addClass("problme_active").siblings().removeClass("problme_active");
					$("#problem div").eq(1).css('color','#fff').siblings().css('color','#000');
					$("#list_problem1").empty();
					$("#list_problem").empty();
					if($('#tab_problem').attr('data-value')==1){
						G_Prg.popMsg('您当前处于背做题模式');
					}else{
						G_Prg.popMsg('您当前处于做题模式');
					}
		        	myProblemRanking(0);
		        } else if(jsonData.status==201){
		        	G_Prg.confirm('该功能充值后才能使用！', function() {
						window.location.href='/recharge';
					});
					return;
		        }
		    }
		    G_AjaxApi_Record.get(URL, false, _success);
		}
	});
}
//答题模式下收藏排行
var count=50;
function myProblemRanking(n) {
	$('#pullUp').css('display','none');
	$('.noNum').css('display','none');
	$('#loadingDiv').css('display','block');
	$("#list_problem").css('display','none');
    $("#list_problem1").css('display','block');
	var _success = function (json) {
    	var jsonData = JSON.parse(json);
    	if(jsonData.status==203){
    		G_Prg.popMsg('购买才能使用此功能');
    	}else if(jsonData.status==202){
    		$('.noNum').css('display','block');
    		$('.noNum').text('获取数据失败');
    	}else if(jsonData.status==200){
    		Count1=jsonData.data.count;
    		var datareplace=G_SetTestInfo.replaceImgURL(jsonData.data.test)
    		var data=datareplace.StyleItems;
    		var info=jsonData.data.info;
    		if(Count1>50){
    			$('#pullUp').css('display','block');
    		}
    		for(var i=0; i<data.length;i++){
    			var infos=jsonData.data.info[i];
    			var title='';
    			var answer='';
    			var list=data[i].TestItems[0];
    			var explain='';
    			if(data[i].Type=='A3TEST'){
    				var titleF=list.A3TestItems[0];
    				title=list.FrontTitle+titleF.Title;
    				var Answers=titleF.Answer.split('');
					for(var j=0;j<titleF.SelectedItems.length;j++){
						for(var s=0;s<Answers.length;s++){
							if(Answers[s]==titleF.SelectedItems[j].ItemName){
								answer+=titleF.SelectedItems[j].Content+';';
							}
						}
    				}
					explain=titleF.Explain;
    			}else if(data[i].Type=='ATEST'||data[i].Type=='BTEST'||data[i].Type=='XTEST'){
    				title=list.Title;
    				answer='';
    				var Answers=list.Answer.split('');
					for(var j=0;j<list.SelectedItems.length;j++){
						for(var s=0;s<Answers.length;s++){
							if(Answers[s]==list.SelectedItems[j].ItemName){
								answer+=list.SelectedItems[j].Content+';';
							}
						}
    				}
					explain=list.Explain;
    			}else{
    				title=list.Title;
    				answer=list.Answer;
					explain=list.Explain;
    			}
    			var strLi = "<li allTestID='"+infos.AllTestID+"' childTableID='"+infos.ChildTableID+"' childTableName='"+data[i].Type+"' cptID='"+infos.CptID+"' srcID='"+infos.SrcID+"' SbjID='"+infos.SbjID+"' StyleID='"+infos.StyleID+"' page='"+n+"'>" 
				+ "<div class='problme_title' data-flase='flase' data-display='none' >" 
				+ "<span id=''>("+info[i].FavCount+")</span><span id=''>"+(i+1+n*50)+".</span><span class='detail_problem'>"+title+"</span>"
				+ "<div class='problme_key pro_key'>"
				+ "<div class='jie_da'><span>答</span>" 
				+ "<p>"+answer+"</p>" 
				+ "</div>" 
				+ "<div class='jie_da jie2'><span>解</span>" 
				+ "<p>"+explain+"</p>"
				+ "</div>" 
				+ "</div>" 
				+ "</li>";
				$("#list_problem1").append(strLi);
		    }
    	}
    		display_none();
    		for(var i=0;i<50;i++){
    			$('.jie2').each(function(){
    				if($(this).find('p').text()==0||$(this).find('p').text()=='null'){
    					$(this).remove();
    				}
    			})
    		}
    		myscroll.refresh();
		 	tiaozhuan();
    }
    var URLs = '/api/testRank/getFavOrErrorRankTest';
    G_AjaxApi_Record.post(URLs,{
    	appID:appID,
//	     userID:UserID,
	     queryType:2,
//	     appEName:'GJZC_ZYX',//appEName
	     curPage:n,
//	     userName:getUserName,
	     guid:getGuid,
//	     cptID:-1,
	     eachPageNum:50
    }, true, _success);
    return 
}
//点击标题显示隐藏 的解析
function display_none() {
	$("#list_problem li").unbind("click").click(function() {
		if($('#tab_problem').attr('data-value')==2){
			tiaozhuan();
		}else{
			if ($(this).children('.problme_title').attr("data-display") == "none") {
				$(this).children('.problme_title').attr("data-display", "block")
				$('#list_problem .pro_key').eq($(this).index()).css("display","block")
				$(this).children('.problme_key').css("display", "block")
			}else{
				$(this).children('.problme_title').attr("data-display", "none")
				$('#list_problem .pro_key').eq($(this).index()).css("display","none")
				$(this).children('.problme_key').css("display", "none")
			}
		}
	})
	$("#list_problem1 li").unbind("click").click(function() {
		if($('#tab_problem').attr('data-value')==2){
			tiaozhuan();
		}else{
			if ($(this).children('.problme_title').attr("data-display") == "none") {
				$(this).children('.problme_title').attr("data-display", "block");
				$('#list_problem1 .pro_key').eq($(this).index()).css("display","block")
			}else{
				$(this).children('.problme_title').attr("data-display", "none");
				$('#list_problem1 .pro_key').eq($(this).index()).css("display","none");
			}
		}
	})
}
function tiaozhuan(){
	$("#list_problem li").on("click", function() {
		var cptID=$(this).attr('cptID')
		var allTestID=$(this).attr('allTestID');
		var page=Number($(this).attr('page'))+1;
		G_Cookie.setCptID(cptID);
		if($('#tab_problem').attr('data-value')==2){
			window.location.href = '/answerExam?fromUrl=searchExam&type=userFav&allTestID='+ allTestID+'&page='+ page + '&pageType=Collect&redoSwitch=' + '0';
		}
	})	
	$("#list_problem1 li").on("click", function() {
		var cptID=$(this).attr('cptID')
		var allTestID=$(this).attr('allTestID');
		var page=Number($(this).attr('page'))+1;
		G_Cookie.setCptID(cptID);
		if($('#tab_problem').attr('data-value')==2){
			window.location.href = '/answerExam?fromUrl=searchExam&type=favRank&allTestID='+ allTestID+'&page='+ page + '&pageType=Collect&redoSwitch=' + '1';
		}
	})	
}
//点击进行收藏
function pullUpAction(){
	if($('#problem .problme_active').index()==0){
		setTimeout(function () {
			m ++;
			if(50*m > Count && 50*(m-1) < Count){
				myProblem(m);
				G_Prg.popMsg('内容已加载完毕');
				$('#pullUp').css('display','none');
			}else if(50*m < Count){
				myProblem(m);
			}
			myscroll.refresh();
	    }, 100);
	}else{
		 setTimeout(function () {
		 	n++;
			if(50*n > Count1 && 50*(n-1) < Count1){
				myProblemRanking(n);
				G_Prg.popMsg('内容已加载完毕');
				$('#pullUp').css('display','none');
			}else{
				myProblemRanking(n);
			}	
			myscroll.refresh();
	    }, 100);
	}
}
function pullDownAction(){
	setTimeout(function () {
	 	$('#pullDown').css('display','none');
		myscroll.refresh();
    }, 100);
}